/*******************************************************************************
 * Copyright (c) 2004, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.reliablesource.pleiades;

import java.util.LinkedList;
import java.util.List;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;
import org.reliablesource.model.core.IReliableElement;
import org.reliablesource.model.core.IReliableNode;

/**
 * Mockup of a browser history view. For now, it's just the sample view
 * generated by PDE.
 */
public class HistoryView extends ViewPart implements ISecondaryPart,
		ISelectionListener {

	private TreeViewer viewer;

	private final List<IReliableElement> selectionList = new LinkedList<IReliableElement>();

	public HistoryView() {
		super();
	}

	@Override
	public void createPartControl(Composite _parent) {

		viewer = new TreeViewer(_parent);

		viewer.setContentProvider(new ITreeContentProvider() {

			public void dispose() {
			}

			public void inputChanged(Viewer _viewer, Object _oldInput,
					Object _newInput) {
			}

			public Object[] getElements(Object _inputElement) {
				if (_inputElement instanceof List) {
					List<IReliableElement> _list = (List<IReliableElement>) _inputElement;
					return _list.toArray();
				}
				return new Object[0];
			}

			public Object[] getChildren(Object _parentElement) {
				if (_parentElement instanceof IReliableNode)
					return ((IReliableNode) _parentElement).getChildren();
				return new Object[0];
			}

			public Object getParent(Object _element) {
				if (_element instanceof IReliableElement)
					return ((IReliableElement) _element).getParent();

				return new Object();
			}

			public boolean hasChildren(Object _element) {
				if (_element instanceof IReliableNode)
					return true;

				return false;
			}

		});

		class SemanticBindingLabelProvider implements IColorProvider,
				ILabelProvider {

			private final List<ILabelProviderListener> labelProviderListenerList = new LinkedList<ILabelProviderListener>();

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.jface.viewers.ITableColorProvider#getBackground(java.lang.Object,
			 *      int)
			 */
			public Color getBackground(Object _element, int _columnIndex) {
				return null;
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.jface.viewers.ITableColorProvider#getForeground(java.lang.Object,
			 *      int)
			 */
			public Color getForeground(Object _element, int _columnIndex) {
				return null;
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object,
			 *      int)
			 */
			public Image getColumnImage(Object _element, int _columnIndex) {
				return null;
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
			 */
			public void addListener(final ILabelProviderListener _listener) {
				if (null != _listener)
					if (!labelProviderListenerList.contains(_listener))
						labelProviderListenerList.add(_listener);
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
			 */
			public void dispose() {
				labelProviderListenerList.clear();
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object,
			 *      java.lang.String)
			 */
			public boolean isLabelProperty(Object _element, String _property) {
				return false;
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
			 */
			public void removeListener(ILabelProviderListener _listener) {
				if (null != _listener)
					if (labelProviderListenerList.contains(_listener))
						labelProviderListenerList.remove(_listener);
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
			 */
			public Color getBackground(Object _element) {
				// TODO Auto-generated method stub
				return null;
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
			 */
			public Color getForeground(Object _element) {
				// TODO Auto-generated method stub
				return null;
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
			 */
			public Image getImage(Object _element) {
				// TODO Auto-generated method stub
				return null;
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
			 */
			public String getText(Object _element) {

				if (_element instanceof ReliableSnippet)
					return ((ReliableSnippet) _element).getDescription();

				else if (_element instanceof IReliableElement)
					return ((IReliableElement) _element).getId();

				return "Unknown type";
			}

		}

		viewer.setLabelProvider(new SemanticBindingLabelProvider());
		viewer.setInput(selectionList);
		getSite().getWorkbenchWindow().getSelectionService()
				.addSelectionListener(this);

		getSite().setSelectionProvider(viewer);
		getSite().registerContextMenu(new MenuManager(), viewer);

	}

	@Override
	public void setFocus() {
		if (viewer != null)
			if (viewer.getTree() != null)
				viewer.getTree().setFocus();

	}

	/**
	 * 
	 * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart,
	 *      org.eclipse.jface.viewers.ISelection)
	 */
	public void selectionChanged(final IWorkbenchPart _part,
			final ISelection _selection) {

		if (!(_part instanceof BrowserView))
			return;

		if (_selection.equals(StructuredSelection.EMPTY))
			return;

		if (_selection instanceof IStructuredSelection) {
			ReliableSnippet _selected = (ReliableSnippet) ((IStructuredSelection) _selection)
					.getFirstElement();

			if (!selectionList.contains(_selected.getParent())) {
				selectionList.add(_selected.getParent());

			}

			if (viewer != null)
				viewer.refresh();
		}

	}

}
